package com.logistic.server.controller;

import cn.hutool.core.util.ArrayUtil;
import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.common.TreeSelect;
import com.logistic.server.entity.pojo.SysDept;
import com.logistic.server.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 字典数据 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "部门报接口", tags = {"部门报接口"})
@Slf4j
@RestController
@LoginRequired
@RequestMapping("/system/dept")
public class SysDeptController  {

    @Resource
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     * @param dept 部门信息
     * @return 结果
     */
    @ApiOperation("获取部门列表")
    @GetMapping
    public Result<List<SysDept>> list(SysDept dept) {
        return ResultGenerator.genSuccessResult(deptService.selectDeptList(dept));
    }

    /**
     * 查询部门列表（排除节点）
     * @param deptId 部门id
     * @return 结果
     */
    @ApiOperation("查询部门列表（排除节点）")
    @GetMapping("/list/exclude/{deptId}")
    public Result<List<SysDept>> excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        depts.removeIf(d -> d.getDeptId().equals(deptId)
                || ArrayUtil.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return ResultGenerator.genSuccessResult(depts);
    }

    /**
     * 根据部门编号获取详细信息
     * @param deptId 部门id
     * @return 结果
     */
    @ApiOperation("根据部门编号获取详细信息")
    @GetMapping(value = "/{deptId}")
    public Result<SysDept> getInfo(@PathVariable Long deptId) {
        return ResultGenerator.genSuccessResult(deptService.selectDeptById(deptId));
    }

    /**
     * 获取部门下拉树列表
     * @param dept 部门
     * @return 结果
     */
    @ApiOperation("获取部门下拉树列表")
    @GetMapping("/treeSelect")
    public Result<List<TreeSelect>> treeSelect(SysDept dept) {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return ResultGenerator.genSuccessResult(deptService.buildDeptTreeSelect(depts));
    }

    /**
     * 加载对应角色部门列表树
     * @param roleId 权限id
     * @return 结果
     */
    @ApiOperation("加载对应角色部门列表树")
    @GetMapping(value = "/roleDeptTreeSelect/{roleId}")
    public Result<Map<String, Object>> roleDeptTreeSelect(@PathVariable("roleId") Long roleId) {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        return ResultGenerator.genSuccessResult(new HashMap<String, Object>(){{
            put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
            put("depts", deptService.buildDeptTreeSelect(depts));
        }});
    }

    /**
     * 新增部门
     * @param dept 部门信息
     */
    @ApiOperation("新增部门")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysDept dept) {
        deptService.insertDept(dept);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 修改部门
     * @param dept 部门
     * @return 结果
     */
    @ApiOperation("修改部门")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysDept dept) {
        deptService.updateDept(dept);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 删除部门
     * @param deptId 部门id
     */
    @ApiOperation("删除部门")
    @DeleteMapping("/{deptId}")
    public Result<?> remove(@PathVariable Long deptId) {
        deptService.deleteDeptById(deptId);
        return ResultGenerator.genSuccessResult();
    }
}
